package com.example.onlinetaxi.ui.activity.RouteInfoPage;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.IExchangeFragments;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.view.CircleWaveView;
import com.example.onlinetaxi.view.Dialog.MyDialog;

import java.util.HashMap;
import java.util.Map;

// 显示用户等待被接单的时长
public class WaitingFragment extends Fragment implements Chronometer.OnChronometerTickListener {

    private View rootView;
    // 取消叫车按钮
    private LinearLayout cancelBtn;
    // 水波组件
    private CircleWaveView circleView;
    // 正计时组件
    private Chronometer mChronometer;
    private int current = 0;  // 从0开始计数
    // fragment切换
    private IExchangeFragments iExchangeFragments;
    // 请求参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_waiting, container, false);

        // 取消按钮(LinearLayout)
        cancelBtn = rootView.findViewById(R.id.cancel_waiting_btn);

        // 获取水波移动组件
        circleView = rootView.findViewById(R.id.circle);

        // 获取正数计时器
        mChronometer = rootView.findViewById(R.id.chronometer);

        // 启动组件
        initEvent();

        return rootView;
    }

    private void initEvent() {
        // 启动水波
        circleView.start();
        // 正数计时设置初始值（重置）
        mChronometer.setBase(0);
        // 正数计时事件监听器，时间发生变化时可进行操作
        mChronometer.setOnChronometerTickListener(this);
        // 启动正数计时器，设置格式(默认"MM:SS"格式)
        mChronometer.start();

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    private void showDialog() {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 向后端发送取消清单请求
                cancelOrder();

                // 清除当前订单
                LocalStorage.currentOrderId = null;

                Toast.makeText(getActivity(), "您已取消叫车!", Toast.LENGTH_SHORT).show();
                iExchangeFragments.exchangeFragment();
            }
        };

        MyDialog.drawable = getContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否停止叫车？", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    private void cancelOrder() {
        // 传入订单
        map.put("orderId", LocalStorage.currentOrderId);

        // 发送取消订单请求
        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "order", "cancelOrder");
    }

    public void initChronometer(String current) {
        int time = Integer.parseInt(current);
        // 正数计时设置初始值（重置）
        this.current = time;
    }

    //正数计时显示格式
    public static String FormatMiss(int time) {
        String mm = (time % 3600) / 60 > 9 ? (time % 3600) / 60 + "" : "0" + (time % 3600) / 60;
        String ss = (time % 3600) % 60 > 9 ? (time % 3600) % 60 + "" : "0" + (time % 3600) % 60;
        return mm + ":" + ss;
    }

    @Override
    public void onChronometerTick(Chronometer chronometer) {
        current++;
        chronometer.setText(FormatMiss(current));
    }

    public void setiExchangeFragments(IExchangeFragments iExchangeFragments) {
        this.iExchangeFragments = iExchangeFragments;
    }
}